<template>
  <el-form
    :model="queryParams"
    ref="queryRef"
    :inline="true"
    label-width="120px"
    class="searchForm"
    @submit.native.prevent
  >
    <el-row :gutter="0">
      <el-col :span="6" v-for="item in options">
        <slot :name="item.prop" :filterForm="queryParams">
          <el-form-item :label="item.label + ':'">
            <el-input
              v-if="
                item.type === 'text' ||
                item.type === 'input' ||
                item.type === 'slot' ||
                item.type === 'textarea'
              "
              @change="handleQuery"
              v-model="queryParams[item.prop]"
              clearable
              style="width: 100%"
              @keyup.enter="handleQuery"
            />
            <el-select
              v-else-if="item.type === 'select' && item.optionItems.length > 0"
              @change="handleQuery"
              v-model="queryParams[item.prop]"
              placeholder=""
              style="width: 100%"
            >
              <el-option
                v-for="ele in item.optionItems"
                :key="ele.value"
                :value="ele.value"
                :label="ele.label"
              ></el-option>
            </el-select>
            <el-date-picker
              v-else-if="item.type === 'date'"
              :type="showTime ? 'datetime' : 'date'"
              @change="handleQuery"
              v-model="queryParams[item.prop]"
              :valueFormat="item.valueFormat"
              :format="item.valueFormat"
              clearable
              style="width: 100%"
              @keyup.enter="handleQuery"
            />
            <el-input
              v-else
              @change="handleQuery"
              v-model="queryParams[item.prop]"
              clearable
              style="width: 100%"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </slot>
      </el-col>

      <slot :filterForm="queryParams" :handleQuery="handleQuery"></slot>
      <el-col :span="4" class="mini250" style="text-align: right">
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script setup>
import { defineProps, defineEmits } from "vue";
const emits = defineEmits(["onQuery", "update:modelValue"]);
const queryParams = ref({});
const props = defineProps({
  modelValue: [Object],
  options: {
    type: Object || Array,
    default: () => [],
  },
  showTime: {
    type: Boolean,
    default: false,
  },
});
watch(
  () => props.modelValue,
  (newData) => {
    queryParams.value = newData;
  },
  {
    deep: true,
    immediate: true,
  }
);
const handleQuery = (val) => {
  updateModel();
  emits("onQuery", queryParams.value);
};
const resetQuery = () => {
  queryParams.value = {};
  updateModel();
  emits("onQuery", queryParams.value);
};

const updateModel = () => {
  emits("update:modelValue", queryParams.value);
};
</script>

<style lang="scss">
.searchForm {
  .el-form--inline.el-form-item {
    // margin-right: 10px;
  }
  .el-form-item__label {
    text-align: right !important;
    justify-content: end !important;
  }
}
.mini250 {
  min-width: 250px;
}
</style>
